﻿using eLoan.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace eLoan.Controllers
{
    public class AdministrationController : BaseController
    {

        public ActionResult Index()
        {
            try
            {
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Users(bool excludeInactive = true)
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Users);

                var db = new DatabaseContext();

                List<User> users;
                if (excludeInactive)
                    users = db.Users.Where(w => w.IsActive == true).ToList();
                else
                    users = db.Users.ToList();

                var model = new UserListModel();

                model.ExcludeInactive = excludeInactive;
                model.Users = AdministrationHelper.GetUserModels(users).ToList();

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult RefreshUsers(UserListModel model)
        {
            return RedirectToAction("Users", new { excludeInactive = model.ExcludeInactive });
        }

        public ActionResult Branches()
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Branches);

                var db = new DatabaseContext();
                return View(db.Branches.ToList());
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Agents(string branchCode)
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Agents);

                var db = new DatabaseContext();

                branchCode = string.IsNullOrEmpty(branchCode) ? AdministrationHelper.GetLoggedInUserBranchCode(db, User.Identity.Name) : branchCode;

                var model = new AgentListModel();
                model.Branches = AdministrationHelper.GetBranchDropDownList(db.Branches);
                model.SelectedBranchCode = branchCode;

                var agents = db.Agents.Where(w => w.BranchCode == branchCode);
                model.Agents = AdministrationHelper.GetAgentModels(agents).ToList();


                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult RefreshAgents(AgentListModel model)
        {
            return RedirectToAction("Agents", new { branchCode = model.SelectedBranchCode });
        }

        public ActionResult LoanTypes()
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.LoanTypes);

                var db = new DatabaseContext();
                return View(db.LoanTypes.ToList());
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult ChargeTypes()
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.ChargeTypes);

                var db = new DatabaseContext();
                return View(db.ChargeTypes.ToList());
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult PaymentTypes()
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.PaymentTypes);

                var db = new DatabaseContext();
                return View(db.PaymentTypes.ToList());
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Notifications()
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Notifications);

                var db = new DatabaseContext();
                return View(db.Notifications.ToList());
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Banks()
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Banks);

                var db = new DatabaseContext();
                return View(db.Banks.ToList());
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        
    }
}
